<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<!--
	Copyright [2007] [Sunny Liu]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.	
-->    
<sqlMap namespace="${entity.javaName}">
  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="${entity.normalizedJavaName}" type="${appConfig.outputPackageName}.model.${entity.javaName}"/>
  
  <!-- Select with no parameters using the result map for Account class. -->
  <select id="findAll${entity.javaName}" resultClass="${entity.normalizedJavaName}">
    SELECT  
    <#list entity.attributes as attribute>
    ${attribute.columnName} <#if attribute.columnName != attribute.javaName >AS ${attribute.javaName}</#if><#if attribute_has_next>,</#if>
    </#list>
    FROM ${entity.tableName}
  </select>

  <select id="find${entity.javaName}ByKey" parameterClass="${entity.normalizedJavaName}"	resultClass="${entity.normalizedJavaName}">
    SELECT  
    <#list entity.attributes as attribute>
    ${attribute.columnName} <#if attribute.columnName!= attribute.javaName >AS ${attribute.javaName}</#if><#if attribute_has_next>,</#if>
    </#list>
    FROM ${entity.tableName}    
    WHERE 
    <#if entity.hasPrimaryKey >
    <#list entity.primaryKeyColumns as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    <#else>
    <#list entity.attributes as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    </#if>    
  </select>
     
  
  <insert id="add${entity.javaName}" parameterClass="${entity.normalizedJavaName}">
    INSERT INTO ${entity.tableName} (
      <#list entity.attributes as attribute>
      ${attribute.columnName}<#if attribute_has_next>,</#if>
    </#list>
    )  values (
      <#list entity.attributes as attribute>
      #${attribute.javaName}#<#if attribute_has_next>,</#if>
    </#list>    
    )
  </insert>
  
  <update id="update${entity.javaName}" parameterClass="${entity.normalizedJavaName}">
    UPDATE ${entity.tableName} SET
    <#list entity.attributes as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next>,</#if>
    </#list>
     WHERE 
     <#if entity.hasPrimaryKey >
    <#list entity.primaryKeyColumns as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    <#else>
    <#list entity.attributes as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    </#if>
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="delete${entity.javaName}" parameterClass="${entity.normalizedJavaName}">
    DELETE FROM ${entity.tableName}
    WHERE  
    <#if entity.hasPrimaryKey >
    <#list entity.primaryKeyColumns as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    <#else>
    <#list entity.attributes as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
    </#if>
  </delete>
  <#if entity.hasPrimaryKey >	
  <delete id="delete${entity.javaName}ByKey" parameterClass="${entity.normalizedJavaName}">
    DELETE FROM ${entity.tableName}
    WHERE
     <#list entity.primaryKeyColumns as attribute>
    ${attribute.columnName}=#${attribute.javaName}#<#if attribute_has_next> AND </#if>
    </#list>
  </delete>
 </#if>
</sqlMap>